package com.voice.server.controller.device;

import com.voice.server.constant.RoleConstant;
import com.voice.server.entity.Device;
import com.voice.server.enums.DeviceStatusEnum;
import com.voice.server.enums.PushTypeEnum;
import com.voice.server.repository.DeviceRepository;
import com.wangjiacai.utils.output.ResultVO;
import com.wangjiacai.utils.utils.EnumUtil;
import com.wangjiacai.utils.utils.RandomStringUtils;
import com.wangjiacai.utils.utils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceRepository deviceRepository;
    @GetMapping("/status")
    public ResultVO getDeviceStatus(){
        List<Map<String, Object>> list= EnumUtil.getEnumInfo(DeviceStatusEnum.class,"code","status");
        return ResultVOUtil.success(list);
    }

    @GetMapping("/pushType")
    public ResultVO getDevicePushType(){
        List<Map<String, Object>> list= EnumUtil.getEnumInfo(PushTypeEnum.class,"code","type");
        return ResultVOUtil.success(list);
    }

    @PostMapping("/devicekey/deviceSecret")
    public ResultVO postDevicekeyDeviceSecret(String client,String mac){
        if(client==null) return null;
        if(!client.equals("mxchip")) return null;
        Device device=new Device();
        device.setStatus(DeviceStatusEnum.Offline.getCode());
        device.setDeviceKey(mac);
        device.setDeviceSecret(RandomStringUtils.randomString(32));
        device.setUserId(RoleConstant.rootUserId);
        deviceRepository.save(device);
        return ResultVOUtil.success(device);
    }

}
